package ext.acae2.dms.parse.wc9;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.Node;

import ext.acae2.dms.bean.EPMDocumentBean;
import ext.acae2.util.CmIniFileUtil;
import ext.acae2.util.LoadingXmlUtil;
import ext.acae2.util.StringUtil;

public class ParseEPMDocument {

	@SuppressWarnings("unchecked")
	public static EPMDocumentBean execute(String path, String attrPathFolder) throws Exception{
		EPMDocumentBean epmDocumentBean = new EPMDocumentBean();
		Map<String, String> map = new HashMap<String, String>();
		
		Document doc = LoadingXmlUtil.executeForDom4j(path);
		Node localIDNode = doc.selectSingleNode("/EPMDocument/ObjectID/localId");
		String localId = localIDNode.getText();
		String epmDocId = localId.substring(localId.lastIndexOf(":") + 1, localId.length());
		epmDocumentBean.setId(epmDocId);
		
		Node ownerApplicationNode = doc.selectSingleNode("/EPMDocument/ownerApplication");
		String ownerApplication = ownerApplicationNode.getText();
		epmDocumentBean.setOwnerApplication(ownerApplication);
		
		Node authoringApplicationNode = doc.selectSingleNode("/EPMDocument/authoringApplication");
		String authoringApplication = authoringApplicationNode.getText();
		epmDocumentBean.setAuthoringApplication(authoringApplication);
		
		Node numberNode = doc.selectSingleNode("/EPMDocument/number");
		String number = numberNode.getText();
		epmDocumentBean.setNumber(number);
		
		Node nameNode = doc.selectSingleNode("/EPMDocument/name");
		String name = nameNode.getText();
		epmDocumentBean.setName(name);
		
		String masteredText = doc.getRootElement().elementText("masterUfid");
		String masterId = masteredText.substring(masteredText.indexOf(":")+1,masteredText.indexOf("|"));
		epmDocumentBean.setMasterId(masterId);
		
		Node CADNameNode = doc.selectSingleNode("/EPMDocument/CADName");
		String CADName = CADNameNode.getText();
		epmDocumentBean.setCadName(CADName);
		
		Node epmDocTypeNode = doc.selectSingleNode("/EPMDocument/epmDocType");
		String epmDocType = epmDocTypeNode.getText();
		epmDocumentBean.setType(epmDocType);
		
		Node versionIdNode = doc.selectSingleNode("/EPMDocument/versionInfo/versionId");
		String versionId = versionIdNode.getText();
		Node iterationIdNode = doc.selectSingleNode("/EPMDocument/versionInfo/iterationId");
		String iterationId = iterationIdNode.getText();
		String version = versionId + "." + iterationId;
		epmDocumentBean.setVersion(version);
		
		List<Node> contentIdNodes = doc.selectNodes("/EPMDocument/contentItem/contentId");
		List<Node> categoryNodes = doc.selectNodes("/EPMDocument/contentItem/category");
		for (int i = 0; i < contentIdNodes.size(); i++) {
			String ref = contentIdNodes.get(i).getText();
			if(ref.endsWith(".prt")){
				String category = categoryNodes.get(i).getText();
				epmDocumentBean.setLocationRef((path.substring(0,
						path.lastIndexOf(File.separator) + 1) + ref).replaceAll("\\", "/"));
				epmDocumentBean.setCategory(category);
			}
		}
		
		List<Node> ibaPathNodes = doc.selectNodes("/EPMDocument/iba/ibaPath");
		List<Node> ibaValueNodes = doc.selectNodes("/EPMDocument/iba/ibaValue");
		for (int i = 0; i < ibaPathNodes.size(); i++) {
			String title = ibaPathNodes.get(i).getText();
			String value = ibaValueNodes.get(i).getText();
			if(!StringUtil.isEmpty(value)){
				map.put(title, value);
			}
		}
		
		ibaPathNodes = doc.selectNodes("/EPMDocument/excludedIba/ibaPath");
		ibaValueNodes = doc.selectNodes("/EPMDocument/excludedIba/ibaValue");
		for (int i = 0; i < ibaPathNodes.size(); i++) {
			String title = ibaPathNodes.get(i).getText();
			String value = ibaValueNodes.get(i).getText();
			if(!value.equals("((null))")){
				map.put(title, value);
			}
		}
		
		Map<String, String> resultMap = new HashMap<String, String>();
		CmIniFileUtil ini = new CmIniFileUtil(attrPathFolder);
		 
		Set<String> keys = ini.getSection("Attr Requird").keySet();
		for(String key:keys){
			String tcAttrName = ini.getSection("Attr Requird").get(key, "");
			Set<String> attrValueKeys = ini.getSection(key).keySet();
			if(attrValueKeys.size() != 0){
				for(String attrValueKey:attrValueKeys){
					String tcAttrValue = ini.get(attrValueKey, "");
					if(map.get(tcAttrName).equals(tcAttrValue)){
						resultMap.put(key, attrValueKey==null?"":attrValueKey);
					}
				}
			}else{
				resultMap.put(key, map.get(tcAttrName)==null?"":map.get(tcAttrName));
			}
		}
		
		epmDocumentBean.setMap(resultMap);
		return epmDocumentBean;
	}
	
}
